﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Xmobile.Libs;
using CNDM.DB;

namespace CNDM.BO.Category
{
    public class CategoryData
    {
        public static DataTable Get_Category(int Cat_ID)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.XMobile_Category_SelectBy_CatParent(Cat_ID, Constants.Channel);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.XMobile_Category_SelectBy_CatParent(Cat_ID, Constants.Channel);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    dt.Columns.Add("Cat_Url");
                    string cat_url = "";
                    for(int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["Cat_DisplayUrl"].ToString()!="")
                            cat_url = "/" + dt.Rows[i]["Cat_DisplayUrl"].ToString() + Constants.Ext;
                        else
                            cat_url = "/" + Lib.UnicodeToKoDauAndGach(dt.Rows[i]["Cat_Name"].ToString()) + Constants.Ext;

                        dt.Rows[i]["Cat_Url"] = cat_url;
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_Category, cacheName, dt);

            return dt;
        }

        public static DataTable Get_Category_by_CatUrl(string cat_displayurl)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_Category_by_CatUrl(cat_displayurl);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Xmobile_Select_Category_by_DisplayURL(cat_displayurl);
                    db.Close();
                    db.Dispose();
                }
                /*
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt.Columns.Add("Cat_Url");
                    string cat_url = "";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["Cat_DisplayUrl"].ToString() != "")
                            cat_url = "/" + dt.Rows[i]["Cat_DisplayUrl"].ToString() + Constants.Ext;
                        else
                            cat_url = "/" + Lib.UnicodeToKoDauAndGach(dt.Rows[i]["Cat_Name"].ToString()) + Constants.Ext;

                        dt.Rows[i]["Cat_Url"] = cat_url;
                    }

                    dt.AcceptChanges();
                }
                */
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_Category, cacheName, dt);

            return dt;
        }

        public static DataTable Get_All_Category()
        {
            DataTable dt = new DataTable();
            string cacheName = "Get_All_Category_" + Constants.Channel;

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Xmobile_Get_All_Category(Constants.Channel);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    dt.Columns.Add("Cat_Url");
                    string cat_url = "";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["Cat_DisplayUrl"].ToString() != "")
                            cat_url = "/" + dt.Rows[i]["Cat_DisplayUrl"].ToString() + Constants.Ext;
                        else
                            cat_url = "/" + Lib.UnicodeToKoDauAndGach(dt.Rows[i]["Cat_Name"].ToString()) + Constants.Ext;

                        dt.Rows[i]["Cat_Url"] = cat_url;
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_Category, cacheName, dt);

            return dt;
        }
    }
}
